package com.sanyang.pda.presenter;

import com.sanyang.pda.bean.AgvBean;
import com.sanyang.pda.bean.BindBgvinfoBean;
import com.sanyang.pda.bean.TakeStockListBean;
import com.sanyang.pda.bean.request.BingAGVBean;
import com.sanyang.pda.contract.LssuedSetContract;
import com.sanyang.pda.event.RefreshBean;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import io.reactivex.functions.Consumer;

public class LssuedSetPresenter extends BasePresenter<LssuedSetContract .View> implements LssuedSetContract.Presenter {
    @Override
    public void getDeviceBaseInfoByType() {
        NetUtils.geApi().getDeviceBaseInfoByType("0")
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<AgvBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<AgvBean>> takeStockListBeanBaseAppEntity) throws Exception {
                        getView().setList(takeStockListBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void getPadBindBgvinfoByPdaSn(String pdaSN) {
        NetUtils.geApi().getPadBindBgvinfoByPdaSn(pdaSN)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<BindBgvinfoBean>>() {
                    @Override
                    public void accept(BaseAppEntity<BindBgvinfoBean> takeStockListBeanBaseAppEntity) throws Exception {
                        if (takeStockListBeanBaseAppEntity.getData() != null){
                            getView().setBindBean(takeStockListBeanBaseAppEntity.getData());
                        }else {
                            getView().nobBind();
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void getPDABindAGV(String pdaSN, String agvkey) {
        BingAGVBean bingAGVBean=new BingAGVBean();
        bingAGVBean.setAgvKey(agvkey);
        bingAGVBean.setPdasn(pdaSN);
        NetUtils.geApi().getPDABindAGV(bingAGVBean)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<String>>() {
                    @Override
                    public void accept(BaseAppEntity<String> takeStockListBeanBaseAppEntity) throws Exception {
                        getView().showMessage(takeStockListBeanBaseAppEntity.getMessage());
                        EventBus.getDefault().post(new RefreshBean());
                        getView().finishActivity();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void PDAlnbindAGv(String pdaSN) {
        NetUtils.geApi().PDAlnbindAGv(pdaSN)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<String>>() {
                    @Override
                    public void accept(BaseAppEntity<String> takeStockListBeanBaseAppEntity) throws Exception {
                        getView().showMessage(takeStockListBeanBaseAppEntity.getMessage());
                        EventBus.getDefault().post(new RefreshBean());
                        getView().nobBind();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
